﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DBConnection
/// </summary>
public class DBConnection
{
	public DBConnection()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    public void MoKn()
    {
        //con.ConnectionString = "Server=.\\sqlexpress; database=shophoatuoi; integrated security=true";
        con.ConnectionString =@" Data Source=.\SQLEXPRESS;AttachDbFilename=|datadirectory|\Shophoatuoi.mdf;Integrated Security=True;User Instance=True";
        con.Open();
    }
    void Close_con()
    {
        con.Close();
    }
    public DataTable Get_data(string sql, params SqlParameter[] pars)
    {
        DataTable dt = new DataTable();
        cmd.Connection = con;
        cmd.CommandText = sql;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Clear();
        cmd.Parameters.AddRange(pars);
        MoKn();
        SqlDataReader dr = cmd.ExecuteReader();
        dt.Load(dr);
        dr.Close();
        con.Close();
        return dt;
    }

    public object LayDuLieu(string sql, params SqlParameter[] pars)
    {
        cmd.Connection = con;
        cmd.CommandText = sql;

        cmd.Parameters.Clear();
        cmd.Parameters.AddRange(pars);

        MoKn();
        object obj = cmd.ExecuteScalar();
        con.Close();
        return obj;
    }
    public void thuchienlenh(string sql, params SqlParameter[] pars)
    {
        cmd.Connection = con;
        cmd.CommandText = sql;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Clear();
        cmd.Parameters.AddRange(pars);
        MoKn();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}
